<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-row :gutter="20">
                <el-col :span="24">
                    <el-form-item label="专栏名称" prop="specColumnName">
                        <el-input v-model="ruleForm.specColumnName" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="显示/隐藏" prop="status">
                        <el-select v-model="ruleForm.status" class="w100p" clearable filterable placeholder="请选择">
                            <el-option label="显示" :value="true"></el-option>
                            <el-option label="隐藏" :value="false"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="黑白名单" prop="rosterType">
                        <el-select v-model="ruleForm.rosterType" class="w100p" clearable filterable placeholder="请选择">
                            <el-option v-for="item, index in enumList" :key="index" :label="item.text"
                                :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="跳转链接" prop="url">
                        <el-input v-model="ruleForm.url" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="置顶排序" prop="transverse">
                        <el-input type="number" v-model="ruleForm.transverse" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="专栏封面" prop="coverId">
                        <UpLoadImage v-if="dialogVisible" @getFileIdList="getFileIdList" :idList="fileIdList"
                            :limit="1">
                        </UpLoadImage>
                    </el-form-item>
                </el-col>
                
                <el-col :span="24" v-if="ruleForm.rosterType !== 'RELEASE'">
                    <el-form-item label="Ip区间" prop="ipAdressVoList">
                        <div class="IpModule" v-for="item, index in ruleForm.ipAdressVoList" :key="index">
                            <IpModule @change="(ip) => getIp(ip, index, 'startIp')" :ipAddress="item.startIp + ''">
                            </IpModule>
                            <span>至</span>
                            <IpModule @change="(ip) => getIp(ip, index, 'endIp')" :ipAddress="item.endIp + ''">
                            </IpModule>
                            <!-- <span class="label">开始段:</span>
                            <el-input type="number" class="input" v-model="item.startInt" maxlength="3"
                                @input="item.startInt = item.startInt.replace(/[^\d]/g, '')"
                                @blur="syncIp(item.startInt, index, 'startInt')" />
                            <span class="label">结束段:</span>
                            <el-input type="number" class="input" v-model="item.endInt" maxlength="3"
                                @input="item.endInt = item.endInt.replace(/[^\d]/g, '')"
                                @blur="syncIp(item.endInt, index, 'endInt')" /> -->
                            <div class="btnList">
                                <span v-show="ruleForm.ipAdressVoList.length == index + 1"
                                    @click="addIp(index)">添加</span>
                                <span class="del" v-show="ruleForm.ipAdressVoList.length > 1"
                                    @click="delIp(index)">删除</span>
                            </div>
                        </div>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import $baseUrl from '@/utils/baseURL';
import specColumn from "@/api/specColumn"
import UpLoadImage from "@/components/UpLoadImage";
import Kindeditor from '@/components/Kindeditor';
import IpModule from '@/components/IpModule';

export default {
    components: { IpModule, Kindeditor, UpLoadImage },
    // components: { Quill, IpModule, Kindeditor },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        userList: {
            type: Array,
            default: () => {
                return []
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        navigationList: {
            type: Array,
            default: () => {
                return []
            }
        },
        enumjumpList: {
            type: Array,
            default: () => {
                return []
            }
        },
        enumList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {

        // 自定义验证规则，检查网址是否为有效的URL
        var validateUrl = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('网址不能为空'));
            }
            // 正则表达式判断网址格式
            const reg = /(http|https):\/\/([\w.]+\/?)\S*/
            if (reg.test(value)) {
                callback();
            } else {
                callback('请输入正确的网址');
            }
        };
        return {
            sizeList: [
                {
                    name: '八号',
                    size: '6px'
                }, {
                    name: '七号',
                    size: '7px'
                }, {
                    name: '小六',
                    size: '8px'
                }, {
                    name: '六号',
                    size: '10px'
                }, {
                    name: '小五',
                    size: '12px'
                }, {
                    name: '五号',
                    size: '14px'
                }, {
                    name: '小四',
                    size: '16px'
                }, {
                    name: '四号',
                    size: '18px'
                }, {
                    name: '小三',
                    size: '20px'
                }, {
                    name: '三号',
                    size: '21px'
                }, {
                    name: '小二',
                    size: '24px'
                }, {
                    name: '二号',
                    size: '29px'
                }, {
                    name: '小一',
                    size: '32px'
                }, {
                    name: '一号',
                    size: '34px'
                }, {
                    name: '小初',
                    size: '48px'
                }, {
                    name: '初号',
                    size: '56px'
                }


            ],
            fileIdList: [],
            uploadJson: `${$baseUrl.fileServiceUrl}portalWebsite/files/upload`, //上传图片的接口地址
            editorTextCopy: '',
            resetUEditor: true,
            resetIp: true,
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                coverId: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                transverse: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                specColumnName: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                titleSize: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                operator: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                publishTime: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                rosterType: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                coverId: [
                    { required: true, message: '请上传图片', trigger: 'change' }
                ],
                url: [
                    { required: true, message: '请输入', trigger: 'change' },
                    { validator: validateUrl, trigger: 'blur' }
                ],
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() {
        window.addEventListener('message', function (msg) {
            console.log(msg);
        })
    },

    methods: {
        onContentChange(val) {
            this.editorTextCopy = val;
            console.log(this.editorTextCopy)
        },
        getFileIdList(data) {
            let id = data.fileIdList && data.fileIdList.length > 0 ? data.fileIdList[0] : ''
            this.$set(this.ruleForm, 'coverId', id)
        },
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.$set(this.ruleForm, 'status', true);
                this.$set(this.ruleForm, 'rosterType', 'RELEASE');
                this.$set(this.ruleForm, 'jumpType', 'ARTICLE');
                this.$set(this.ruleForm, 'content', '');
                this.$set(this.ruleForm, 'whetherRedHead', false);

                this.addEditApi = specColumn.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = {
                    ...this.data,
                }
                this.addEditApi = specColumn.edit;
                this.addEditName = '编辑';
                this.$set(this.ruleForm, 'ipAdressVoList', this.data.ipAdressVoList);
                this.fileIdList = this.data.coverId ? [this.data.coverId] : [];
            }
            if (!this.ruleForm.ipAdressVoList || this.ruleForm.ipAdressVoList.length == 0) {
                this.$set(this.ruleForm, 'ipAdressVoList', [{ startIp: '', endIp: '' }]);
            }
            this.resetIp = false;
            this.$nextTick(() => {
                this.resetIp = true;
            });
        },
        editIp(strList) {
            let list = []
            strList.forEach(val => {
                if (val) {
                    const ipList = val.split("-");
                    list.push(ipList)
                }
            })
            return [['', '']]
        },
        getIp(ip, index, key) {
            if (!this.ruleForm.ipAdressVoList[index]) {
                this.$set(this.ruleForm.ipAdressVoList, index, { startIp: '', endIp: '' })
            }
            this.$set(this.ruleForm.ipAdressVoList[index], key, ip)
        },
        addIp(index) {
            this.$set(this.ruleForm.ipAdressVoList, index + 1, { startIp: '', endIp: '' })
        },
        delIp(index) {
            this.ruleForm.ipAdressVoList.splice(index, 1)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({
                        ...this.ruleForm,
                        ipAdressVoList: this.ruleForm.rosterType == 'RELEASE' ? [] : this.ruleForm.ipAdressVoList
                    }).then(res => {
                        let success = res.code == 0
                        let msg = success ? '操作成功' : '操作失败'
                        this.$message[success ? 'success' : 'warning'](res.msg || msg)
                        success && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.quillContent {
    min-height: 300px;
}

.IpModule {
    display: flex;
    justify-content: flex-start;
    flex-wrap: nowrap;
    line-height: 38px;

    .input {
        width: 200px;
    }

    .label {
        display: inline-block;
        width: 100px;
        padding: 0 5px;
        text-align: right;
        cursor: pointer;
    }

    .btnList {
        width: 200px;
        display: flex;
        justify-content: flex-start;
        padding-left: 15px;
        color: rgb(27, 143, 244);

        span {
            padding-left: 10px;
        }

        .del {
            color: red;
            padding-right: 5px;
        }
    }
}

.sizeStyle {
    height: 80px;
    line-height: 80px;
}
</style>